//
//  FYEmoticonToolBar.swift
//  Mircore
//
//  Created by apple on 16/8/17.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit


enum FYEmoticonToolBarButtonType: Int{
    //默认表情
    case Normal = 1000
    
    //emoji表情
    
    case Emoji = 1001
    
    //浪小花表情
    
    case LXH = 1002
}

//表情键盘的toolbar

class FYEmoticonToolBar: UIStackView {

    //点击toolbar按钮执行的闭包
    
    var  didSeletedButtonClosure: ((type: FYEmoticonToolBarButtonType) -> ())?
   
    
    //记录上一次的选中按钮
    
    var lastSelectedButton: UIButton?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required  init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder: ) has not been implemented")
    }
    
    private  func   setupUI() {
    
    
        //水平方向布局
        
        axis = .Horizontal
        
        //等比填充
        
        distribution = .FillEqually
    
        addChildButton("默认", imageName: "compose_emotion_table_left", type: .Normal)
        
        addChildButton("Emoji", imageName: "compose_emotion_table_mid", type: .Emoji)
        
        addChildButton("浪小花", imageName: "compose_emotion_table_right", type: .LXH)
        
     }

    
    //添加子按钮的通用方法
    
    private  func  addChildButton(title: String, imageName: String, type: FYEmoticonToolBarButtonType)  {
    
    
    let  button = UIButton()
        
        //根据枚举的原始值为tag
        
        button.tag = type.rawValue
        
        //添加点击事件
        
        button.addTarget(self, action: #selector(FYEmoticonToolBar.buttonAction(_:)), forControlEvents: .TouchUpInside)
        
      button.setTitle(title, forState: .Normal)
        
        //设置不同 状态的文字颜色
        
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        
        
        //设置不同状态的背景图片
        
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: .Selected)
        
        //设置字体大小
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //去掉高亮
        
        button.adjustsImageWhenHighlighted = false
        
        addArrangedSubview(button)
     }
    
    //MARK:-点击事件
    
    
    @objc  private func  buttonAction(button: UIButton)  {
    
      //如果上次点击的选中的按钮和这次点击的按钮一样  那么就直接返回  不执行后面的代码
        
        if  lastSelectedButton  == button  {
        
        
          return
        }
    
        //上一次的按钮取消选中
        
        lastSelectedButton?.selected = false
        
        
        //设置选中状态
        
        button.selected = true
        
        //保存选中的按钮
        
        lastSelectedButton  = button
        
        //通过枚举的原始值获取枚举
        
        let  type = FYEmoticonToolBarButtonType(rawValue: button.tag)!
        
        //执行点击闭包
        
        didSeletedButtonClosure?(type: type)
        
     }
    
    
    //通过指定的section选中响应的按钮
    
    func  selectedButtonWithSection(section: Int){
        
      //通过tag获取相应的按钮  建议以后给控件设置tag的时候不要设置0  因为0获取不到指定的类型控件  获取的是自身
        
        let  button = viewWithTag(section + 1000)  as! UIButton
        
//        if  lastSelectedButton == button {
//        
//         return
//        
//        }
        
        if  lastSelectedButton?.tag  == section + 1000 {
        
          return
        }
        
        
        lastSelectedButton?.selected = false
        
        button.selected  = true
        
        lastSelectedButton = button
    }
}
